package com.stockz.equity.charting.event.listener;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.stockz.equity.charting.display.Displayable;
import com.stockz.equity.charting.event.ChangeTimeUnitEvent;
import com.stockz.equity.charting.event.EventPublisher;
import com.stockz.equity.charting.event.RefreshStockChartDataEvent;
import com.stockz.equity.charting.view.StockChart;
import com.stockz.equity.charting.view.StockChartPanelState;
import com.stockz.equity.manager.BarManager;
import com.stockz.equity.model.Bar;

@Component
public class RefreshChartingDataListener implements ApplicationListener<RefreshStockChartDataEvent> {

	@Autowired private BarManager barManager;
	
	@Override
	public void onApplicationEvent(RefreshStockChartDataEvent event) {
		StockChartPanelState chartingPanelState = ((StockChart)event.getSource()).getStockChartPanel().getStockChartPanelState();
		List<Bar> barList = barManager.getBarListByScripCode(chartingPanelState.getScrip().getScripCode());
		chartingPanelState.getBarList().clear();
		chartingPanelState.getBarList().addAll(barList);
		chartingPanelState.getBackupBarList().clear();
		chartingPanelState.getBackupBarList().addAll(barList);
		chartingPanelState.getShapeSet().clear();
		for(Displayable indicatorDisplay : chartingPanelState.getDisplayableSet()){
			indicatorDisplay.refreshValues();
		}
		EventPublisher.publishEvent(
				new ChangeTimeUnitEvent(
						(StockChart)event.getSource()));
	}
}
